<template>
  <div class="cardManager">
    <div class="search">
      <el-form ref="form" :model="searchParam" label-width="80px" inline>
        <el-form-item label="标题">
          <el-input v-model="searchParam.title"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="searchParam.types" placeholder="请选择" multiple>
            <el-option
                v-for="(item,index) in type"
                :key="index"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>

        </el-form-item>

        <el-form-item label="创建时间">
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择日期" v-model="searchParam.startTime" style="width: 100%;" format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择时间" v-model="searchParam.endTime" style="width: 100%; " format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-col>
        </el-form-item>
        <div class="sub">
          <el-button type="success" @click="chageData(true)">新增</el-button>
          <el-button type="primary" @click="getHot">查询</el-button>
          <el-button>取消</el-button>
        </div>

      </el-form>
    </div>
    <div class="dataTable">
      <el-table :data="hotList"  border max-height="450" >
        <el-table-column type="index" label="序号" width="120"/>
        <el-table-column  label="标题" width="200" prop="title">

        </el-table-column>
        <el-table-column label="内容"  width="400" >
          <template slot-scope="scope">
            <div style="overflow: hidden; height: 50px; width: 400px" >
              <span v-html="scope.row.content" style="height: 30px; overflow: hidden"></span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型"  :formatter="formatStatus">

        </el-table-column>
        <el-table-column label="是否可用"  width="120">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.isView"
                active-color="#13ce66"
                disabled
                inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createAt" label="时间"  width="200"/>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="editHotInfo(scope.row)">编辑</el-button>
            <el-button type="danger" v-text="scope.row.isView?'禁用':'启用'" @click="chageStatus(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">

        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page=searchParam.queryPageDTO.currentPage
            :page-sizes="[10, 50, 100, 200]"
            :page-size=searchParam.queryPageDTO.pageSize
            layout="total, sizes, prev, pager, next, jumper"
            :total="hotListSize">
        </el-pagination>
      </div>
    </div>

    <div>
      <el-dialog
          title="添加和修改"
          :visible.sync="dialogVisible"
          width="70%"
          center>
        <AddTopic :hotObj="hotObj" @chageData="chageData" :cardType="type"></AddTopic>
      </el-dialog>
    </div>

  </div>
</template>
<script>
import {Message} from "element-ui";
import AddTopic from '@c/common/AddTopic'
export default {

  components:{
    AddTopic
  },
  data(){
    return{
      searchParam:{
        queryPageDTO: {
          currentPage: 1,
          pageSize: 10
        }
      },

      hotListSize:null,
      hotList:[],

      dialogVisible:false,
      hotObj:{},
      id:'',
      name:'',
      type:[],
      typeList:[]
    }
  },
  methods:{
    getHot (){

      this.postRequestJson("/api/tg/hot/getTopic", this.searchParam).then(resp=>{
        this.hotList= resp.data.data
        this.hotListSize = resp.data.size
      })
    },
    formatStatus(row) {
      let text = this.type.find(item => item.value === row.type)
      return text.label
    },

    editHotInfo(data) {
      this.dialogVisible = true
      this.hotObj = data
    },
    chageData(flag) {
      this.hotObj ={}
      this.hotObj.isView = true
      this.dialogVisible = flag;
      if (!flag) {
        this.getHot()
      }
    },
    chageStatus(data) {
      this.$confirm('是否改变状态', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        data.isView = !data.isView
        this.postRequestJson("api/tg/hot/addOrEditHotTopic",data).then(resp=>{
          if (resp.data.code === '0') {
            Message.success("操作成功")
          }
        })
      })
    },
    getType () {
      this.getRequest("/api/tg/hot/getType",{}).then(resp=>{
        this.type = resp.data.data
      })
    },

    handleSizeChange(val) {
      this.searchParam.queryPageDTO.pageSize = val
      this.getHot()
    },

    handleCurrentChange(val){
      this.searchParam.queryPageDTO.currentPage = val
      this.getHot()
    }
  },
  created() {
    this.getHot()
    this.getType()
  },
}
</script>
<style>
.cardManager{
  margin-left: 50px;
}
.cardManager .search{
  padding: 30px;
  margin-top: 100px;
  border: #d4caca 1px solid;
}
.cardManager .search .sub{
  margin-top: 60px;
}
.cardManager .dataTable{
  margin-top: 60px;
}
</style>